﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Utils;

namespace COFE.Core
{
    /// <summary>
    /// Used by Web Services, user can access the all metadata (e.g. \contents\{id}\metadata)
    /// </summary>
    public class MetadataAttribute : Attribute
    {        
        private bool _majorProperty = false;
     
        /// <summary>
        /// Whether it is a major property? if yes, it will embed it when returning COFE.Web.Entry to web services.
        /// </summary>
        public bool MajorProperty { get { return _majorProperty; } set { _majorProperty = value; } }

        public MetadataAttribute(bool majorProperty)
        {           
            MajorProperty = majorProperty;
        }


        public MetadataAttribute()
            : this(false)
        {

        }

    }

    /// <summary>
    /// Extends MetadataAttribute.
    /// </summary>
    public static class MetadataExtension
    {
        /// <summary>
        /// Whether a property is tagged with MetadataAttribute.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        /// <returns></returns>
        public static bool IsMetadata<T>(T item)
        {
            return EnumAttributeUtils<MetadataAttribute, T>.FindAttribute(item) != null;
        }


        /// <summary>
        /// Get MetadataAttribute that tagged with the specified property.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        /// <returns></returns>
        public static MetadataAttribute getMetadata<T>(T item)
        {
            return EnumAttributeUtils<MetadataAttribute, T>.FindAttribute(item);            
        }


        /// <summary>
        /// Return whether the specified property is major property.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        /// <returns></returns>
        public static bool getIsMajorProperty<T>(T item)
        {
            return EnumAttributeUtils<MetadataAttribute, T>.FindAttribute(item).MajorProperty;
        }
          
    }
}
